/*
 *  Copyright 2009 Allan Lykke Christensen.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package dk.i2m.netbeans.modules.doxia;

import java.io.IOException;
import org.netbeans.core.spi.multiview.MultiViewDescription;
import org.netbeans.core.spi.multiview.MultiViewFactory;
import org.openide.cookies.EditCookie;
import org.openide.cookies.EditorCookie;
import org.openide.cookies.OpenCookie;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.text.CloneableEditorSupport;
import org.openide.text.DataEditorSupport;

/**
 * Support for editing and rendering APT files.
 *
 * @author Allan Lykke Christensen
 */
public class AptEditorSupport extends DataEditorSupport implements
        OpenCookie, EditorCookie, EditCookie {

    //        CloneableTopComponent tc = MultiViewFactory.createCloneableMultiView(descriptions, rendered, null);
    //    tc.setDisplayName(entry.getDataObject().getName());
    final MultiViewDescription[] descriptions = {
        new AptSourceDescription(this),
        new AptViewDescription(this)};

    private AptEditorSupport(AptDataObject obj) {
        super(obj, new AptEnv(obj));
    }

    @Override
    protected CloneableEditorSupport.Pane createPane() {
        return (CloneableEditorSupport.Pane) MultiViewFactory.
                createCloneableMultiView(descriptions, descriptions[0]);
    }

    private static final class AptEnv extends DataEditorSupport.Env implements
            SaveCookie {

        public AptEnv(AptDataObject obj) {
            super(obj);
        }

        public void save() throws IOException {
            AptEditorSupport ed =
                    (AptEditorSupport) findCloneableOpenSupport();
            ed.saveDocument();
        }

        protected FileObject getFile() {
            return super.getDataObject().getPrimaryFile();
        }

        protected FileLock takeLock() throws IOException {
            return ((AptDataObject) super.getDataObject()).getPrimaryEntry().
                    takeLock();
        }
    }

    public static AptEditorSupport create(AptDataObject obj) {
        return new AptEditorSupport(obj);
    }

    @Override
    protected boolean notifyModified() {
        boolean retValue;
        retValue = super.notifyModified();
        if (retValue) {
            AptDataObject obj =
                    (AptDataObject) getDataObject();
            obj.ic.add(env);
        }
        return retValue;
    }

    @Override
    protected void notifyUnmodified() {
        super.notifyUnmodified();
        AptDataObject obj = (AptDataObject) getDataObject();
        obj.ic.remove(env);
    }
    

}
